package com.ptu.service.impl;

import com.ptu.entity.PageTools;
import com.ptu.entity.Punishment;
import com.ptu.mapper.PunishmentMapper;
import com.ptu.service.PunishmentService;
import com.ptu.util.DBUtil;

import java.util.List;

public class PunishmentServicelmpl implements PunishmentService {
    @Override
    public List<Punishment> findPunMore(String punname, String pundate) {
        PunishmentMapper mapper = DBUtil.getSqlSession().getMapper(PunishmentMapper.class);
        List<Punishment> puns = mapper.selectPunMore(punname, pundate);
        DBUtil.closeAll();
        return puns;
    }

    @Override
    public PageTools<Punishment> findPunAll(int pageSize, int pageNumber) {
        PunishmentMapper mapper = DBUtil.getSqlSession().getMapper(PunishmentMapper.class);

        PageTools<Punishment> pageTools = new PageTools<>();
        pageTools.setPageNumber(pageNumber);
        pageTools.setPageSize(pageSize);

        int pageStart = pageSize*(pageNumber-1);
        List<Punishment> pun = mapper.selectPage(pageSize, pageStart);
        int total = mapper.selectPunCount();
        pageTools.setTotal(total);
        pageTools.setList(pun);

        DBUtil.closeAll();
        return pageTools;
    }

    @Override
    public int addPunishment(Punishment pun) {
        PunishmentMapper mapper = DBUtil.getSqlSession().getMapper(PunishmentMapper.class);
        int insertpunment = mapper.insertPunishment(pun);
        DBUtil.closeAll();
        return insertpunment;
    }

    @Override
    public int removePun(Integer punid) {
        PunishmentMapper mapper = DBUtil.getSqlSession().getMapper(PunishmentMapper.class);
        int i = mapper.delectPun(punid);
        DBUtil.closeAll();
        return i;
    }

    @Override
    public int changePun(Punishment pun) {
        PunishmentMapper mapper = DBUtil.getSqlSession().getMapper(PunishmentMapper.class);
        int updatepun = mapper.updatePun(pun);
        DBUtil.closeAll();
        return updatepun;
    }

    @Override
    public Punishment findPunByPid(Integer punid) {
        PunishmentMapper mapper = DBUtil.getSqlSession().getMapper(PunishmentMapper.class);
        Punishment pun = mapper.selectPunByPid(punid);
        DBUtil.closeAll();
        return pun;
    }

    @Override
    public List<Punishment> findAll() {
        PunishmentMapper mapper = DBUtil.getSqlSession().getMapper(PunishmentMapper.class);
        List<Punishment> pun = mapper.selectAll();
        DBUtil.closeAll();
        return pun;
    }
}
